package com.pro.system.Controller;

import com.pro.system.Repository.PurchaseRepository;
import com.pro.system.entity.Movie;
import com.pro.system.entity.Purchase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("admin/purchases")
public class PurchaseController {

    @Autowired
    PurchaseRepository purchaseRepository;

    @GetMapping
    public String showMovie (Model model){
        List<Purchase> purchases  = purchaseRepository.findAll();
        model.addAttribute("purchase",purchases);
        return "web/background/purchases/purchases";
    }

    @GetMapping("intonew")
    public String intonew (Model model){
        Purchase purchase = new Purchase();
        model.addAttribute(purchase);
        return "web/background/purchases/purchases-new";
    }

    @PostMapping("save")
    public String save(Model model,Purchase purchase ){
        Purchase save = purchaseRepository.save(purchase);
        if(save != null){
            return "web/background/purchases/purchases-new";
        }
        else {
            return "";
        }
    }

    @PostMapping("deleteItemByCheck")
    public String deleteItemByCheck(Model model, Integer[] getvalues){
        for(int i=0; i<getvalues.length;++i){
            purchaseRepository.deleteById(getvalues[i]);
        }
        return "redirect:/admin/purchases";
    }

    @PostMapping("research")
    public String research(Model model,@RequestParam String ByNameLike){
        List<Purchase> ByNameLikes = purchaseRepository.findByNameLike(ByNameLike);
        model.addAttribute("purchase",ByNameLikes);
        return  "web/background/purchases/purchases";
    }
}
